package GUILayer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import ModelLayer.*;
import ControlLayer.*;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class Employee extends JFrame {

	private JPanel contentPane;
	private JTextField txtSalaryHour;
	private JTextField txtHoursWeek;
	private JTextField txtFName;
	private JTextField txtLName;
	private JTextField txtCPR;
	private JTextField txtAddress;
	private JTextField txtCity;
	private JTextField txtZIP;
	private JTextField txtPhone;
	private JTextField txtMail;
	private ModelLayer.Employee e;
	
	
	private boolean errorP;
	private boolean errorZ;
	private boolean errorS;
	private boolean errorH;
	
	private IFSearch search;
	private IFCreate create;
	private IFUpdate update;

	/**
	 * Create the frame.
	 */
	public Employee(final boolean isNew, boolean isArchive, final int id) {
	
		search = new SearchControler();
		update = new UpdateControler();
		create = new CreateControler();
		
		setResizable(false);
		if(isNew){
			setTitle("New employee");
		}
		else{
			setTitle("Employee");
			e = search.getEmployeeById(id);
			}
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(150, 50, 300, 380);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(255, 255, 204));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblId = new JLabel("ID:");
		lblId.setBounds(10, 11, 89, 14);
		contentPane.add(lblId);
		
		JLabel lblFirstName = new JLabel("First name:");
		lblFirstName.setBounds(10, 36, 89, 14);
		contentPane.add(lblFirstName);
		
		JLabel lblLastName = new JLabel("Last name:");
		lblLastName.setBounds(10, 61, 89, 14);
		contentPane.add(lblLastName);
		
		JLabel lblCpr = new JLabel("CPR:");
		lblCpr.setBounds(10, 86, 89, 14);
		contentPane.add(lblCpr);
		
		JLabel lblAddress = new JLabel("Address:");
		lblAddress.setBounds(10, 111, 89, 14);
		contentPane.add(lblAddress);
		
		JLabel lblCity = new JLabel("City:");
		lblCity.setBounds(10, 136, 89, 14);
		contentPane.add(lblCity);
		
		JLabel lblZipCode = new JLabel("ZIP code:");
		lblZipCode.setBounds(10, 161, 89, 14);
		contentPane.add(lblZipCode);
		
		JLabel lblPhone = new JLabel("Phone:");
		lblPhone.setBounds(10, 186, 89, 14);
		contentPane.add(lblPhone);
		
		JLabel lblEmail = new JLabel("E-mail:");
		lblEmail.setBounds(10, 211, 89, 14);
		contentPane.add(lblEmail);
		
		JLabel lblSalaryhour = new JLabel("Salary/hour:");
		lblSalaryhour.setBounds(10, 236, 89, 14);
		contentPane.add(lblSalaryhour);
		
		JLabel lblHoursweek = new JLabel("Hours/week:");
		lblHoursweek.setBounds(10, 261, 89, 14);
		contentPane.add(lblHoursweek);
		
		JLabel lblRole = new JLabel("Role:");
		lblRole.setBounds(10, 286, 89, 14);
		contentPane.add(lblRole);
		
		JLabel lblShowID = new JLabel("");
		lblShowID.setBounds(121, 11, 118, 14);
		contentPane.add(lblShowID);
		lblShowID.setText(Integer.toString(id));
		
		txtFName = new JTextField();
		txtFName.setBounds(121, 33, 118, 20);
		contentPane.add(txtFName);
		txtFName.setColumns(10);
		if(!isNew)
		{
			txtFName.setText(e.getName());
			if(isArchive)
			{
				txtFName.setEditable(false);
			}
		}
		
		txtLName = new JTextField();
		txtLName.setBounds(121, 58, 118, 20);
		contentPane.add(txtLName);
		txtLName.setColumns(10);
		if(!isNew)
		{
			txtLName.setText(e.getSurname());
			if(isArchive)
			{
				txtLName.setEditable(false);
			}
		}
		
		txtCPR = new JTextField();
		txtCPR.setBounds(121, 83, 118, 20);
		contentPane.add(txtCPR);
		txtCPR.setColumns(10);
		if(!isNew)
		{
			txtCPR.setText(e.getCPR());
			if(isArchive)
			{
				txtCPR.setEditable(false);
			}
		}
		
		txtAddress = new JTextField();
		txtAddress.setBounds(121, 108, 118, 20);
		contentPane.add(txtAddress);
		txtAddress.setColumns(10);
		if(!isNew)
		{
			txtAddress.setText(e.getAddress());
			if(isArchive)
			{
				txtAddress.setEditable(false);
			}
		}
		
		txtCity = new JTextField();
		txtCity.setBounds(121, 133, 118, 20);
		contentPane.add(txtCity);
		txtCity.setColumns(10);
		if(!isNew)
		{
			txtCity.setText(e.getCity());
			if(isArchive)
			{
				txtCity.setEditable(false);
			}
		}
		
		txtZIP = new JTextField();
		txtZIP.setBounds(121, 158, 118, 20);
		contentPane.add(txtZIP);
		txtZIP.setColumns(10);
		if(!isNew)
		{
			txtZIP.setText(e.getZipCode());
			if(isArchive)
			{
				txtZIP.setEditable(false);
			}
		}
		
		txtPhone = new JTextField();
		txtPhone.setBounds(121, 183, 118, 20);
		contentPane.add(txtPhone);
		txtPhone.setColumns(10);
		if(!isNew)
		{
			txtPhone.setText(e.getPhoneNO());
			if(isArchive)
			{
				txtPhone.setEditable(false);
			}
		}
		
		txtMail = new JTextField();
		txtMail.setBounds(121, 208, 118, 20);
		contentPane.add(txtMail);
		txtMail.setColumns(10);
		if(!isNew)
		{
			txtMail.setText(e.getEMail());
			if(isArchive)
			{
				txtMail.setEditable(false);
			}
		}
		
		txtSalaryHour = new JTextField();
		txtSalaryHour.setBounds(121, 233, 118, 20);
		contentPane.add(txtSalaryHour);
		txtSalaryHour.setColumns(10);
		if(!isNew)
		{
			txtSalaryHour.setText(Double.toString(e.getSalaryPerHour()));
			if(isArchive)
			{
				txtSalaryHour.setEditable(false);
			}
		}
		
		txtHoursWeek = new JTextField();
		txtHoursWeek.setBounds(121, 258, 118, 20);
		contentPane.add(txtHoursWeek);
		txtHoursWeek.setColumns(10);
		if(!isNew)
		{
			txtHoursWeek.setText(Double.toString(e.getHoursPerWeek()));
			if(isArchive)
			{
				txtHoursWeek.setEditable(false);
			}
		}
		
		
		final JComboBox comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"Manager", "Warehouse", "Office", "Delivery"}));
		comboBox.setBounds(121, 283, 118, 20);
		contentPane.add(comboBox);
		if(!isNew)
		{
			switch(e.getRole())
			{
				case "manager": comboBox.setSelectedIndex(0);
					break;
				case "warehouse": comboBox.setSelectedIndex(1);
					break;
				case "office": comboBox.setSelectedIndex(2);
					break;
				case "delivery": comboBox.setSelectedIndex(3);
					break;
			}
			if(isArchive)
			{
				comboBox.setEditable(false);
			}
		}
		
		
		if(!isArchive)
		{
			JButton btnSave = new JButton("Save");
			btnSave.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(isNew){
						saveNewEmployee(id, txtFName.getText(), txtLName.getText(), txtCPR.getText(), txtAddress.getText(), txtCity.getText(), txtZIP.getText(), txtPhone.getText(), txtMail.getText(), comboBox.getSelectedItem().toString(), txtSalaryHour.getText(), txtHoursWeek.getText());
					}
					else
					{
						updateEmployee(id, txtFName.getText(), txtLName.getText(), txtCPR.getText(), txtAddress.getText(), txtCity.getText(), txtZIP.getText(), txtPhone.getText(), txtMail.getText(), comboBox.getSelectedItem().toString(), txtSalaryHour.getText(), txtHoursWeek.getText());
					}
					dispose();
				}
			});
			btnSave.setBounds(10, 317, 89, 23);
			contentPane.add(btnSave);
		}
		else
		{
			JButton btnActivate = new JButton("Activate");
			btnActivate.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent arg0){
					e.setStatus("active");
					activateEmployee(e);
					MainWindow.getInstance().refreshTableEmployee();
					MainWindow.getInstance().refreshAC();
				}
			});
			btnActivate.setBounds(10, 317, 89, 23);
			contentPane.add(btnActivate);
			
		}
		
			
			JButton btnCancel = new JButton("Cancel");
			btnCancel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					dispose();
				}
			});
			btnCancel.setBounds(179, 317, 89, 23);
			contentPane.add(btnCancel);
		
		
		
	}
	
	private void activateEmployee(ModelLayer.Employee e)
	{
		update.updateEmployee(e);
		dispose();
	}
	
	private void saveNewEmployee(int id, String fName, String lName, String cpr, String address, String city, String zip, String phone, String eMail, String role, String salaryHour, String hoursWeek)
	{
		if(fName.isEmpty() || lName.isEmpty() || cpr.isEmpty() || address.isEmpty() || city.isEmpty() || zip.isEmpty() || phone.isEmpty() || eMail.isEmpty() || role.isEmpty())
		{
			JOptionPane.showMessageDialog(null,
                    "All fields have to be filled.",
                    "Warning",
                    JOptionPane.WARNING_MESSAGE);
		}
		else
		{
		    errorP=false;
            try
            {
                    Integer.valueOf(phone);
            }
            catch(Exception e)
            {
                    JOptionPane.showMessageDialog(null,
                                    "The phone number should be a number!",
                                    "Warning",
                                    JOptionPane.WARNING_MESSAGE);
                    errorP=true;
            }

            if(errorP!=true)
            {
                errorZ=false;
                try
                {
                        Integer.valueOf(zip);
                }
                catch(Exception e)
                {
                        JOptionPane.showMessageDialog(null,
                                        "The ZIP code should be a number!",
                                        "Warning",
                                        JOptionPane.WARNING_MESSAGE);
                        errorZ = true;
                }

                if(errorZ!=true)
                {
                	errorS = false;
                	try
                	{
                		Double.valueOf(salaryHour);
                	}
                	catch(Exception e)
                	{
                		JOptionPane.showMessageDialog(null,
                                "The salary should be a number!",
                                "Warning",
                                JOptionPane.WARNING_MESSAGE);
                		errorS = true;
                	}
                	if(errorS!=true)
                	{
                		errorH = false;
                		try
                		{
                			Double.valueOf(hoursWeek);
                		}
                		catch(Exception e)
                		{
                			JOptionPane.showMessageDialog(null,
                                    "The ZIP code should be a number!",
                                    "Warning",
                                    JOptionPane.WARNING_MESSAGE);
                			errorH = true;
                		}
                		if(errorH!=true)
                		{
                			double salary = Double.valueOf(salaryHour);
                			double hours = Double.valueOf(hoursWeek);
                			ModelLayer.Employee e = new ModelLayer.Employee(id, address, city, zip, phone, eMail, "active", cpr, fName, lName, role, salary, hours);
                			try {
								create.createEmployee(e);
							} catch (Exception e1) {
								JOptionPane.showMessageDialog(null,
		                                "Something went wrong, please try again!",
		                                "Warning",
		                                JOptionPane.WARNING_MESSAGE);
								e1.printStackTrace();
							}
                			MainWindow.getInstance().refreshTableEmployee();
                			dispose();
                		}
                	}
                }
            } 
		}
	}
	
	private void updateEmployee(int id, String fName, String lName, String cpr, String address, String city, String zip, String phone, String eMail, String role, String salaryHour, String hoursWeek)
	{
		if(fName.isEmpty() || lName.isEmpty() || cpr.isEmpty() || address.isEmpty() || city.isEmpty() || zip.isEmpty() || phone.isEmpty() || eMail.isEmpty() || role.isEmpty())
		{
			JOptionPane.showMessageDialog(null,
                    "All fields have to be filled.",
                    "Warning",
                    JOptionPane.WARNING_MESSAGE);
		}
		else
		{
		    errorP=false;
            try
            {
                    Integer.valueOf(phone);
            }
            catch(Exception e)
            {
                    JOptionPane.showMessageDialog(null,
                                    "The phone number should be a number!",
                                    "Warning",
                                    JOptionPane.WARNING_MESSAGE);
                    errorP=true;
            }

            if(errorP!=true)
            {
                errorZ=false;
                try
                {
                        Integer.valueOf(zip);
                }
                catch(Exception e)
                {
                        JOptionPane.showMessageDialog(null,
                                        "The ZIP code should be a number!",
                                        "Warning",
                                        JOptionPane.WARNING_MESSAGE);
                        errorZ = true;
                }

                if(errorZ!=true)
                {
                	errorS = false;
                	try
                	{
                		Double.valueOf(salaryHour);
                	}
                	catch(Exception e)
                	{
                		JOptionPane.showMessageDialog(null,
                                "The salary should be a number!",
                                "Warning",
                                JOptionPane.WARNING_MESSAGE);
                		errorS = true;
                	}
                	if(errorS!=true)
                	{
                		errorH = false;
                		try
                		{
                			Double.valueOf(hoursWeek);
                		}
                		catch(Exception e)
                		{
                			JOptionPane.showMessageDialog(null,
                                    "The ZIP code should be a number!",
                                    "Warning",
                                    JOptionPane.WARNING_MESSAGE);
                			errorH = true;
                		}
                		if(errorH!=true)
                		{
                			double salary = Double.valueOf(salaryHour);
                			double hours = Double.valueOf(hoursWeek);
                			ModelLayer.Employee e = new ModelLayer.Employee(id, address, city, zip, phone, eMail, "active", cpr, fName, lName, role, salary, hours);
                			update.updateEmployee(e);
                			MainWindow.getInstance().refreshTableEmployee();
                			
                		}
                	}
                }
            } 
		}
	}
}
